package zpf.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import zpf.entity.User;

/**
 * @ClassName: MovieCustomRibbonController
 * @Description: todo
 * @Autor: ZPF
 * @Date: 2021/1/13 11:22
 **/
@RestController
public class MovieCustomRibbonController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        // http://localhost:7900/simple/
        // VIP：virtual IP
        // HAProxy Heartbeat

        return this.restTemplate.getForObject("http://provider-user/simple/" + id, User.class);
    }

    @GetMapping("/choose")
    public String testChoose() {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("provider-user");
        System.out.println("00000" + ":" + serviceInstance.getServiceId() + ":" + serviceInstance.getHost() + ":" + serviceInstance.getPort());

        return "choose successful";
    }

}
